<?php

namespace App\Plugin\HostServer\Admin;



use App\Plugin\HostServer\Models\PnHostSvCategory;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CategoryController extends BaseController
{
    public $pageName = '分类';


    public function module(){
        return [
            [
                'id' => 'server',
                'name' => '主机区域'
            ],
            [
                'id' => 'system',
                'name' => '镜像系统'
            ]
        ];
    }

    public function indexData()
    {
        $item = [];
        $item['all_create_url'] = action($this->route['controller_name'] . '@allCreate', \request()->all());
        $item['all_post_url'] = action($this->route['controller_name'] . '@allCreatePost');
        $item['navs'] =$this->module() ;
        return $item;
    }

    /**
     * 更新层级
     * @param $model
     * @param string $id
     */
    protected function afterSave($model, $id = '')
    {
        if ($model->parent_id != 0) {
            //上级
            $next = $this->model->find($model->parent_id)->path_level;
            $next = $next ? $next . '-' : '';
            $model->path_level = $next . $model->id;
            $model->save();
        } else {
            $model->path_level = $model->id;
            $model->save();
        }
        //PnForumCategory::cacheCategory(1);
        return $model;
    }


    public function createEditData($show = '')
    {
        $group_type = request()->input('group_type');

        if ($show) {
            $group_type = $show->group_type;
        }
        $data = PnHostSvCategory::category($group_type);

        return ['cate' => $data, 'module' => $this->module()];
    }


    public function setModel()
    {
        return $this->model = new PnHostSvCategory();
    }

    public function postData($id = '')
    {
        $data = $this->rq->all();
        if (!$id) {
            $data['ename'] = pinyin_permalink($data['name'], '');
        }

        return $data;
    }

    public function apiJsonItemExtend($item)
    {

        $item['pid'] = $item['parent_id'];
        return $item;
    }

    public function checkRule($id = '')
    {
        if (!$id) {
            return [
                'name' => [
                    "required"
                ],
                'group_type' => 'required',

            ];
        }
        return [
            'name' => [
                "required"
            ],
            'group_type' => 'required',
            'ename' => [
                "required"
            ],

        ];
    }

    public function checkRuleField()
    {
        return ['ename' => '标识符'];
    }

    /**
     * 批量添加设置数据
     * @param Request $request
     * @return array|void
     */
    public function allCreateSetData(Request $request)
    {
        $name_arr = explode("\n", $request->input('name'));
        $ename_arr = explode("\n", $request->input('ename'));
        $all = $request->all();
        $arr = [];
        if (empty($name_arr)) {
            return [];
        }
        $parent_id = $request->input('parent_id');

        foreach ($name_arr as $k => $v) {

            unset($all['_token']);
            unset($all['file']);
            $all['name'] = $v;
            $all['ename'] = pinyin_permalink($all['name'], '');
            $all['parent_id'] = $parent_id;

            $arr[] = $all;
        }

        return $arr;
    }

    //进行层级设置
    public function afterAllCreateEvent()
    {
        $all = $this->model->whereNull('path_level')->get();

        foreach ($all as $k => $v) {
            if ($v['parent_id'] == 0) {
                $v->path_level = $v->id;
                $v->save();
            } else {
                //上级
                $next = $v->parents->path_level;
                $next = $next ? $next . '-' : '';
                $v->path_level = $next . $v->id;
                $v->save();
            }
        }

    }

    /**
     * 批量验证规则
     * @return array
     */
    public function allCreateCheckForm()
    {
        $error = $this->checkForm($this->rq->all(), $this->checkRule(''), $this->checkRuleMsg(), $this->checkRuleField());
        return $error;
    }

    public function checkDelet($id_arr)
    {
        //判断是否有子元素
        $has = $this->model->whereIn('parent_id', $id_arr)->count();

        if ($has > 0) {
            return '存在子级无法删除，请删除子级先';
        }
    }


}
